<!doctype html>
<html>
  <head>
    <title>Background Page</title>
		<script src='js/jquery-1.7.1.min.js'></script>
    <script type="text/javascript">

		function CurrentPage ()
		{
			var me = this;
			var isInit = false;

		  me.Url;
		  me.Domain;
		  me.CurrentTabId;

		  me.callRibbonMethod = function (methodName){
		   	chrome.tabs.sendRequest(me.CurrentTabId, {action: methodName}, function(response){
		   	});
		  }

		  me.updatePageAction = function(tabId, changeInfo){
				chrome.tabs.get(tabId, function (tab){
			    me.CurrentTabId = tabId;
			    me.Url = tab.url;
			    me.Domain = extractDomain(tab.url);
					if(changeInfo.status == "loading") {
						me.callRibbonMethod("loading");
					}
					if (changeInfo.status == "complete") {
					  me.callRibbonMethod("init");
					}
			  	chrome.pageAction.show(tabId);
			  });
		  }
		}
		
		function extractDomain(url){
		  return url.match(/:\/\/(.+?)\//)[1];
		}
		
		var objCurrentPage = new CurrentPage;

		chrome.tabs.onSelectionChanged.addListener(objCurrentPage.updatePageAction);
		chrome.tabs.onUpdated.addListener(objCurrentPage.updatePageAction);
		
		chrome.extension.onRequest.addListener(function(request, sender) {
			// alert(request.redirect);
			var redirectUrl = '';
			switch(request.redirect) {
				case 'mail':
					redirectUrl = 'https://mail.google.com';
					chrome.tabs.update(sender.tab.id, {url: redirectUrl});
					break;
				case 'serp':
					redirectUrl = 'https://www.google.com';
					chrome.tabs.update(sender.tab.id, {url: redirectUrl});
					break;
				case 'search':
					redirectUrl = 'https://www.google.com';
					chrome.tabs.update(sender.tab.id, {url: redirectUrl});
					break;
				case 'maps':
					redirectUrl = 'http://maps.google.com/';
					chrome.tabs.update(sender.tab.id, {url: redirectUrl});
					break;
				case 'plus':
					redirectUrl = 'http://plus.google.com/';
					chrome.tabs.update(sender.tab.id, {url: redirectUrl});
					break;
				case 'news':
					redirectUrl = 'http://news.google.com/';
					chrome.tabs.update(sender.tab.id, {url: redirectUrl});
					break;
				case 'youtube':
					redirectUrl = 'http://www.youtube.com/';
					chrome.tabs.update(sender.tab.id, {url: redirectUrl});
					break;					
			}
			
		});

			
		</script>
  </head>
  <body>

  </body>
</html>
